package com.winit.openapi.isp.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.spi.context.CommandContext;
import com.winit.oms.spi.isp.ValidateISPOrderService;
import com.winit.oms.spi.isp.command.ValidateOrderCommand;
import com.winit.oms.spi.isp.vo.ISPOrder;
import com.winit.oms.spi.vo.ISPOrderErr;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.ValidateUtil;

/**
 * ISP订单收件信息校验
 * 
 * @version Revision History
 * 
 * <pre>
 * Author     Version       Date        Changes
 * long.yue    1.0           2015年1月8日     Created
 * 
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/ispVerifier")
public class ISPVerifierController extends BaseController {

    @Resource
    private ValidateISPOrderService ispValidateService;

    public void setIspValidateService(ValidateISPOrderService ispValidateService) {
        this.ispValidateService = ispValidateService;
    }

    /**
     * 收件信息校验
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/checkRecipInfo")
    public String checkRecipInfo(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {

        Object obj = requestMsg.getData();
        ISPOrder ispOrder = null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ispOrder = buildISPOrder(json);
            checkRecpInfoFields(json);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        ValidateOrderCommand command = new ValidateOrderCommand();
        command.setiSPOrder(ispOrder);
        command.setCtx(CommandContext.getContext());
        List<ISPOrderErr> errList = ispValidateService.validateISPOrderCountry(command, true);
        // Map<String, Object> rData = new HashMap<String, Object>();
        // if(errList.size()==0){
        // rData.put("checkResult", ApiConstant.TRUE);
        // }else{
        // rData.put("checkResult", ApiConstant.FALSE);
        // //错误详情
        // List<Map<String,String>> detail = new
        // ArrayList<Map<String,String>>();
        // for(ISPOrderErr err : errList){
        // Map<String, String> errMap = new HashMap<String, String>();
        // errMap.put("filedName",err.getFiledName());
        // errMap.put("errCode",err.getErrCode());
        // errMap.put("errMsg",err.getErrMsg());
        // detail.add(errMap);
        // }
        // rData.put("errDataScope", detail);
        // }
        // responseMsg.setData(rData);

        if (errList != null && errList.size() > 0) {
            throw new ApiException(ErrorCode.VALIDTE_ORDER_FAILED);
        }
        return SUCCESS;
    }

    /**
     * 收件信息字段校验
     * 
     * @param json
     */
    private void checkRecpInfoFields(JSONObject json) {
        // buyerName
        ValidateUtil.validNotNull(json, "buyerName");
        ValidateUtil.validMaxLength(json, "buyerName", 50);
        // buyerContactNo
        ValidateUtil.validNotNull(json, "buyerContactNo");
        ValidateUtil.validMaxLength(json, "buyerContactNo", 50);
        // buyerEmail
        ValidateUtil.validMaxLength(json, "buyerEmail", 60);
        // buyerZipCode
        ValidateUtil.validNotNull(json, "buyerZipCode");
        ValidateUtil.validMaxLength(json, "buyerZipCode", 10);
        // buyerCountry
        ValidateUtil.validNotNull(json, "buyerCountry");
        ValidateUtil.validMaxLength(json, "buyerCountry", 50);
        // buyerState
        ValidateUtil.validNotNull(json, "buyerState");
        ValidateUtil.validMaxLength(json, "buyerState", 20);
        // buyerCity
        ValidateUtil.validNotNull(json, "buyerCity");
        ValidateUtil.validMaxLength(json, "buyerCity", 50);
        // buyerAddress1
        ValidateUtil.validNotNull(json, "buyerAddress1");
        ValidateUtil.validMaxLength(json, "buyerAddress1", 100);
        // buyerAddress2
        ValidateUtil.validMaxLength(json, "buyerAddress2", 100);
    }

    /**
     * 装配isp
     * 
     * @param json
     * @return
     */
    private ISPOrder buildISPOrder(JSONObject json) {
        ISPOrder order = new ISPOrder();
        order.setBuyerFullname(json.getString("buyerName"));
        order.setBuyerContactNumber(json.getString("buyerContactNo"));
        order.setBuyerPostcode(json.getString("buyerZipCode"));
        order.setBuyerCountry(json.getString("buyerCountry"));
        order.setBuyerState(json.getString("buyerState"));
        order.setBuyerCity(json.getString("buyerCity"));
        order.setBuyerAddress1(json.getString("buyerAddress1"));
        if (json.containsKey("buyerEmail")) {
            order.setBuyerEmail(json.getString("buyerEmail"));
        }
        if (json.containsKey("buyerAddress2")) {
            order.setBuyerAddress2(json.getString("buyerAddress1"));
        }
        return order;
    }
}
